package com.bz.xtcx.manager.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import java.io.File;
import java.io.InputStream;
import java.util.Map;

/**
 * @deprecated
 */
public class ConfUtil {
    /** 日志. */
    private static final Logger logger = LoggerFactory.getLogger(ConfUtil.class);
    /**
     * 转换表格数据成中文
     * @param json
     * @param key
     * @param value
     * @return
     */
    public static String transCh(JSONObject json, String key, String value) {
        if (WzStrUtil.isBlank(value)) {
            return "";
        }
        StringBuilder sb = new StringBuilder("");
        try {
            JSONArray val = JSON.parseArray(value);
            //JSONObject json = JSON.parseObject(strJson);
            JSONArray jsons = json.getJSONArray(key);

            for(Object s : val) {
                for(int i = 0;i<jsons.size();i++) {
                    if(jsons.getJSONObject(i).get(s) != null) {
                        sb.append(jsons.getJSONObject(i).get(s)).append(",");
                    }
                }
            }
            if (0 == sb.length()) {
                return "--";
            } else if (",".equals(sb.charAt(sb.length() - 1))) {
                sb.deleteCharAt(sb.length() - 1);
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        return sb.toString();
    }

    /**
     * 把中文值转换成数字key
     * @param json
     * @param key
     * @param value excel文件 中文字
     * @param other 其它的字段属性
     * @return
     */
    public static String transKeyNo(JSONObject json, String key, String value, String other) {
        StringBuilder sb = new StringBuilder();
        if(value == null || value.equals("")) {
            return "[]";
        }
        try {
            JSONArray jsons = json.getJSONArray(key);
            String [] val = value.replace("\n", "").split("，");//中文逗号,去除换行符
            sb.append("[");
            boolean flag = false;
            boolean flagOther = false;
            StringBuilder sbOther = new StringBuilder();
            first : for(String s : val) {
                flag = false;
                for(int i = 0;i<jsons.size();i++) {
                    if(jsons.getJSONObject(i).get(s) != null) {
                        sb.append(jsons.getJSONObject(i).get(s)).append(",");
                        flag = true;
                        flagOther = true;
                        continue first;
                    }
                }
                if(!flag && !StringUtils.isEmpty(other)) {
                    if(!flagOther) {
                        sb.append("99,");
                    }else {
                        sbOther.append(s).append(",");
                    }
                }

            }
            if(val.length >= 1) {
                sb.deleteCharAt(sb.length() - 1);
            }
            sb.append("]");

        }catch (Exception e) {
            e.printStackTrace();
        }
        return sb.toString();
    }

    public static String transChOnly(JSONObject json, String key, String value) {
        if (WzStrUtil.isBlank(key)) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        try {
            //JSONObject json = JSON.parseObject(strJson);
            JSONArray jsons = json.getJSONArray(key);
            for(int i = 0;i<jsons.size();i++) {
                if(jsons.getJSONObject(i).get(value) != null) {
                    sb.append(jsons.getJSONObject(i).get(value));
                }
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        return sb.toString();
    }

    public static String getPcaaKey(JSONObject json, String val0, String val1, String val2) {
        StringBuilder country = new StringBuilder();
        country.append("[");
        String val = null;
        if(!StringUtils.isEmpty(val0)) {
            val = getPcaaKey(json.getJSONObject("86"), val0);
            country.append("\"").append(val).append("\",");
        }
        if(val != null) {
            val = getPcaaKey(json.getJSONObject(val), val1);
            country.append("\"").append(val).append("\",");
        }
        if(val != null) {
            val = getPcaaKey(json.getJSONObject(val), val2);
            country.append("\"").append(val).append("\"");
        }
        country.append("]");
        return country.toString();
    }

    public static String getPcaaKey(JSONObject json, String val) {
        for (Map.Entry<String, Object> ent : json.entrySet()) {
            if(ent.getValue().equals(val)) {
                return ent.getKey();
            }
        }
        return null;
    }

    public static String getPcaa(JSONObject json, String key) {
        if (WzStrUtil.isBlank(key)) {
            return "";
        }
        StringBuilder sb = new StringBuilder("");
        //String key = "[\"110000\",\"110100\",\"110101\"]";
        try {
            JSONArray val = JSON.parseArray(key);
            sb.append(json.getJSONObject("86").get(val.get(0)));
            sb.append(",").append(json.getJSONObject(val.get(0).toString()).get(val.get(1)));
            sb.append(",").append(json.getJSONObject(val.get(1).toString()).get(val.get(2)));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sb.toString();
    }

    public JSONObject getPcaaJson()  {
        JSONObject pcaaJson = null;
        try {
            InputStream stream = getClass().getClassLoader().getResourceAsStream("pcaa.json");
            File targetFile = new File("conf/pcaa.json");
            FileUtils.copyInputStreamToFile(stream, targetFile);
            String jsonStr = FileUtils.readFileToString(targetFile, "UTF-8");
//			String path = this.getClass().getClassLoader().getResource("pcaa.js").toString();
//			System.out.println(path);
//			if (path.contains(":")) {
//				path = path.replace("file:/","");// 2
//			}
//			String input = FileUtils.readFileToString(new File(path), "UTF-8");
//            input = input.substring(15, input.length());
//            json = JSON.parseObject(input);
            pcaaJson = JSON.parseObject(jsonStr);
        } catch (Exception e) {
            logger.error("读取省市区文件异常", e);
        }
        return pcaaJson;
    }
}
